<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="null">
    <meta name="keyword" content="null">
    <meta name="theme-color" content="#600090">
    <meta name="msapplication-navbutton-color" content="#600090">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="#600090">
    <link rel="shortcut icon" href="/assets/favicon.ico">
    <link rel="alternate" type="application/atom+xml" title="张小阳" href="/atom.xml">
    <link rel="stylesheet" href="https://cdn.bootcss.com/animate.css/3.5.2/animate.css">
    <link rel="stylesheet" href="https://cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.css">
    <title>
        
        Review深入理解Javascript系列(一)｜MillZhang&#39;s blog
        
    </title>

    <link rel="canonical" href="http://millzhang.github.io/2017/06/12/2017-note/11.深入理解javascript-50-1/">

    <!-- Bootstrap Core CSS -->
    <link rel="stylesheet" href="/css/bootstrap.min.css">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/css/blog-style.css">

    <!-- Pygments Github CSS -->
    <link rel="stylesheet" href="/css/syntax.css">
</head>

<!-- hack iOS CSS :active style -->
<body ontouchstart="" class="animated fadeIn">
<!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top " id="nav-top" data-ispost = "true" data-istags="false
" data-ishome = "false" >
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand animated pulse" href="/">
                <span class="brand-logo">
                    MillZhang
                </span>
            </a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <!-- Known Issue, found by Hux:
            <nav>'s height woule be hold on by its content.
            so, when navbar scale out, the <nav> will cover tags.
            also mask any touch event of tags, unfortunately.
        -->
        <!-- /.navbar-collapse -->
        <div id="huxblog_navbar">
            <div class="navbar-collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <a href="/"><i class="fa fa-home"></i>主页</a>
                    </li>
                   <li>
                        <a href="/tags"><i class="fa fa-tags"></i>归档</a>
                    </li>
                    <li>
                        <a href="/about"><i class="fa fa-user"></i>关于</a>
                    </li>
                </ul>
            </div>
        </div>
    </div>
    <!-- /.container -->
</nav>
<script>
    // Drop Bootstarp low-performance Navbar
    // Use customize navbar with high-quality material design animation
    // in high-perf jank-free CSS3 implementation
//    var $body   = document.body;
    var $toggle = document.querySelector('.navbar-toggle');
    var $navbar = document.querySelector('#huxblog_navbar');
    var $collapse = document.querySelector('.navbar-collapse');

    $toggle.addEventListener('click', handleMagic)
    function handleMagic(e){
        if ($navbar.className.indexOf('in') > 0) {
        // CLOSE
            $navbar.className = " ";
            // wait until animation end.
            setTimeout(function(){
                // prevent frequently toggle
                if($navbar.className.indexOf('in') < 0) {
                    $collapse.style.height = "0px"
                }
            },400)
        }else{
        // OPEN
            $collapse.style.height = "auto"
            $navbar.className += " in";
        }
    }
</script>

<!-- Main Content -->

<!--only post-->

<header class="intro-header">
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1 text-center">
                <div class="post-heading">
                    <h1>Review深入理解Javascript系列(一)</h1>
                    
                    <h2 class="subheading">编写高质量的Javascript代码</h2>
                    
                    <span class="meta">
                         作者 MillZhang
                        <span>
                          日期 2017-06-12
                         </span>
                    </span>
                    <div class="tags text-center">
                        
                        <a class="tag" href="/tags/#javascript"
                           title="javascript">javascript</a>
                        
                        <a class="tag" href="/tags/#深入理解Javascript系列"
                           title="深入理解Javascript系列">深入理解Javascript系列</a>
                        
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="post-title-haojen">
        <span>
            Review深入理解Javascript系列(一)
        </span>
    </div>
    <div class="post-header-image">
        
            <img src="http://oritfw5nq.bkt.clouddn.com/1b32d50871844232d16f38b3f3ab93f4.jpg?imageView2/0/format/jpg/interlace/1/q/85|watermark/2/text/QnkgTWlsbHpoYW5n/font/5b6u6L2v6ZuF6buR/fontsize/480/fill/I0ZERkRGRA==/dissolve/56/gravity/SouthEast/dx/10/dy/10|imageslim">
         
    </div>
</header>

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">
            <!-- Post Container -->
            <div class="col-lg-8 col-lg-offset-1 col-sm-9 post-container">
                <h3 id="说明"><a href="#说明" class="headerlink" title="说明"></a>说明</h3><p>系列文章是以下文章的读后感,有<code>copy</code>部分,也有自己练笔和理解部分,特此说明,以后不再赘述:</p>
<ul>
<li><a href="http://www.cnblogs.com/TomXu/archive/2011/12/15/2288411.html" target="_blank" rel="external">汤姆大叔深入理解Javascript系列</a></li>
<li><a href="https://www.kancloud.cn/kancloud/deep-understand-javascript/43686" target="_blank" rel="external">看云</a></li>
</ul>
<h3 id="最小全局变量Minimizing-Global"><a href="#最小全局变量Minimizing-Global" class="headerlink" title="最小全局变量Minimizing Global"></a>最小全局变量Minimizing Global</h3><p>全局变量命名易与第三方的脚本引起冲突,所以尽可能少的使用全局变量是很重要的.相关策略有:命名空间模式或是函数立即自动执行，但是要想让全局变量少最重要的还是始终使用var来声明变量。</p>
<a id="more"></a>
<h3 id="几种不经意之间声明的全局变量示例-反例"><a href="#几种不经意之间声明的全局变量示例-反例" class="headerlink" title="几种不经意之间声明的全局变量示例(反例)"></a>几种不经意之间声明的全局变量示例(反例)</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//第一种</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">sum</span>(<span class="params">x,y</span>)</span>&#123;</div><div class="line">    result = x+y;<span class="comment">//隐式声明了全局变量result</span></div><div class="line">    <span class="keyword">return</span> result;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">//第二种</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span>&#123;</div><div class="line">    <span class="comment">//这边是首先声明了全局变量b,赋值0,又将b的引用指向a;</span></div><div class="line">    <span class="keyword">var</span> a=b=<span class="number">0</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="隐式全局变量与明确定义的全局变量的差别"><a href="#隐式全局变量与明确定义的全局变量的差别" class="headerlink" title="隐式全局变量与明确定义的全局变量的差别"></a>隐式全局变量与明确定义的全局变量的差别</h3><blockquote>
<p>通过<code>var</code>声明的全局变量,通过<code>delete</code>操作符是无法删除的</p>
<p>没有通过<code>var</code>声明的隐式全局变量,可以通过<code>delete</code>删除</p>
</blockquote>
<p>注:<br>在技术上，隐式全局变量并不是真正的全局变量，但它们是全局对象的属性。属性是可以通过delete操作符删除的，而变量是不能的</p>
<h3 id="全局对象的访问"><a href="#全局对象的访问" class="headerlink" title="全局对象的访问"></a>全局对象的访问</h3><p>在浏览器中,全局对象可以通过<code>window</code>的属性来访问.</p>
<p>在任何层级的作用域中访问全局对象:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> global = (<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">    <span class="keyword">return</span>  <span class="keyword">this</span>;</div><div class="line">&#125;());</div><div class="line"><span class="comment">//这个方法可以随时访问全局对象,因为其被当做函数调用了,this总是指向全局对象.</span></div></pre></td></tr></table></figure>
<h3 id="声明变量的小技巧-单var形式"><a href="#声明变量的小技巧-单var形式" class="headerlink" title="声明变量的小技巧:单var形式"></a>声明变量的小技巧:单<code>var</code>形式</h3><p>举个栗子吧:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span>&#123;</div><div class="line">    <span class="keyword">var</span> a=<span class="number">1</span>,</div><div class="line">        b=<span class="number">2</span>,</div><div class="line">        c=a+b,</div><div class="line">        obj=&#123;&#125;,</div><div class="line">        $dom = <span class="built_in">document</span>.getElementById(<span class="string">'demo'</span>),</div><div class="line">        k,m;</div><div class="line">    <span class="comment">//....</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="预解析-var的散布问题-Hosting-A-Problem-with-Scattered-vars"><a href="#预解析-var的散布问题-Hosting-A-Problem-with-Scattered-vars" class="headerlink" title="预解析:var的散布问题(Hosting:A Problem with Scattered vars)"></a>预解析:<code>var</code>的散布问题(Hosting:A Problem with Scattered vars)</h3><p>对于JavaScript，只要你的变量是在同一个作用域中（同一函数），它都被当做是声明的，即使是它在var声明前使用的时候。</p>
<p>栗子:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">a = <span class="string">'hehehe'</span>;</div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span>&#123;</div><div class="line">    <span class="built_in">console</span>.log(a);<span class="comment">//undefined</span></div><div class="line">    <span class="keyword">var</span> a = <span class="string">'abcd'</span>;<span class="comment">//a在此被当作了局部变量,虽然是在之后声明的</span></div><div class="line">    <span class="built_in">console</span>.log(a);<span class="comment">//abcd</span></div><div class="line">&#125;</div><div class="line"></div><div class="line">foo();</div></pre></td></tr></table></figure>
<p>Javascript代码执行分为两个阶段:</p>
<blockquote>
<p>1.变量,函数声明以及正常格式的参数创建,这是一个解析和进入上下文的阶段<br>2.代码执行,函数表达式和不合格的标识符被创建.</p>
</blockquote>
<h3 id="for循环的优化-For-Loops"><a href="#for循环的优化-For-Loops" class="headerlink" title="for循环的优化(For Loops)"></a><code>for</code>循环的优化(For Loops)</h3><p><code>HTMLCollections</code>指<code>DOM</code>方法返回的对象.如:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">document</span>.getElementsByName();</div><div class="line"><span class="built_in">document</span>.getElementsByClassName();</div><div class="line"><span class="built_in">document</span>.getElementsByTagName();</div><div class="line"><span class="built_in">document</span>.images;</div><div class="line"><span class="built_in">document</span>.links;</div><div class="line"><span class="built_in">document</span>.forms;</div><div class="line"><span class="built_in">document</span>.forms[<span class="number">0</span>].elements;<span class="comment">//页面第一个表单的所有域</span></div></pre></td></tr></table></figure>
<p>HTML集合,在每次访问其长度时会实时的查询<code>DOM</code>,<code>DOM</code>操作相对来说比较昂贵.</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//优化的for循环写法,缓存数组的长度.</span></div><div class="line"><span class="keyword">for</span>(<span class="keyword">var</span> i=<span class="number">0</span>,len=array.length;i&lt;len;i++)&#123;</div><div class="line">    <span class="comment">//...</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>还有进一步的微操作:</p>
<ul>
<li>少了一个变量</li>
<li>向下数到0，通常更快，因为和0做比较要比和数组长度或是其他不是0的东西作比较更有效率</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"></div><div class="line"><span class="keyword">var</span> i,myArray=[];</div><div class="line"><span class="keyword">for</span>(i=myArray.length;i--)&#123;</div><div class="line"></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">var</span> myarray = [],</div><div class="line">    i = myarray.length;</div><div class="line"><span class="keyword">while</span> (i–-) &#123;</div><div class="line">   <span class="comment">// 使用myarray[i]做点什么</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="for-in循环-for-in-loops"><a href="#for-in循环-for-in-loops" class="headerlink" title="for-in循环(for in loops)"></a><code>for-in</code>循环(for in loops)</h3><blockquote>
<p><code>for-in</code>循环数组是不推荐的,因为如果数组对象已经被自定义的功能增强,就会发生逻辑错误,同时属性列表的序列的顺序也是不能保证的,<code>for-in</code>适用于对象的循环遍历.</p>
</blockquote>
<p>关于第一句话的理解:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//首先定义了一个对象obj,拥有三个属性</span></div><div class="line"><span class="keyword">var</span> obj = &#123;</div><div class="line">    <span class="attr">a</span>:<span class="number">0</span>,</div><div class="line">    <span class="attr">b</span>:<span class="number">1</span>,</div><div class="line">    <span class="attr">plus</span>:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line"></div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">// 在某处给全局的对象添加了一个原型属性</span></div><div class="line"><span class="keyword">if</span>(<span class="keyword">typeof</span> <span class="built_in">Object</span>.prototype.minus == <span class="string">'undefined'</span>)&#123;</div><div class="line">    <span class="built_in">Object</span>.prototype.minus = &#123;&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">// 使用for-in增强循环遍</span></div><div class="line"><span class="keyword">for</span>(<span class="keyword">var</span> i <span class="keyword">in</span> obj)&#123;</div><div class="line">    <span class="built_in">console</span>.log(i,<span class="string">":"</span>,obj[i]);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">// 过滤</span></div><div class="line"><span class="keyword">for</span>(<span class="keyword">var</span> i <span class="keyword">in</span> obj)&#123;</div><div class="line">    <span class="keyword">if</span>(obj.hasOwnProperty(i))&#123;</div><div class="line">         <span class="built_in">console</span>.log(i,<span class="string">":"</span>,obj[i]);</div><div class="line">    &#125;</div><div class="line">   </div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">//另外一种使用hasOwnProperty()的形式是取消Object.prototype上的方法</span></div><div class="line"><span class="keyword">for</span> (<span class="keyword">var</span> i <span class="keyword">in</span> obj) &#123;</div><div class="line">   <span class="keyword">if</span> (<span class="built_in">Object</span>.prototype.hasOwnProperty.call(obj, i)) &#123; <span class="comment">// 过滤</span></div><div class="line">      <span class="built_in">console</span>.log(i, <span class="string">":"</span>, obj[i]);</div><div class="line">   &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p><a href="http://jsbin.com/nijulos/edit?js,console" target="_blank" rel="external">查看代码</a></p>
<h3 id="内置原型的扩展"><a href="#内置原型的扩展" class="headerlink" title="内置原型的扩展"></a>内置原型的扩展</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="built_in">Object</span>.protoype.myMethod !== <span class="string">"function"</span>) &#123;<span class="comment">//有必要的判断</span></div><div class="line">   <span class="built_in">Object</span>.protoype.myMethod = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</div><div class="line">      <span class="comment">// 实现...</span></div><div class="line">   &#125;;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="switch模式"><a href="#switch模式" class="headerlink" title="switch模式"></a><code>switch</code>模式</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> inspect_me = <span class="number">0</span>,</div><div class="line">    result = <span class="string">''</span>;</div><div class="line"><span class="keyword">switch</span> (inspect_me) &#123;</div><div class="line"><span class="keyword">case</span> <span class="number">0</span>:</div><div class="line">   result = <span class="string">"zero"</span>;</div><div class="line">   <span class="keyword">break</span>;</div><div class="line"><span class="keyword">case</span> <span class="number">1</span>:</div><div class="line">   result = <span class="string">"one"</span>;</div><div class="line">   <span class="keyword">break</span>;</div><div class="line"><span class="keyword">default</span>:</div><div class="line">   result = <span class="string">"unknown"</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="避免隐式类型判断"><a href="#避免隐式类型判断" class="headerlink" title="避免隐式类型判断"></a>避免隐式类型判断</h3><p>建议使用<code>===</code>或<code>!==</code>比较值</p>
<h3 id="避免eval"><a href="#避免eval" class="headerlink" title="避免eval()"></a>避免<code>eval()</code></h3><blockquote>
<p><code>eval()</code>此方法接受任意的字符串，并当作JavaScript代码来处理;</p>
</blockquote>
<h3 id="parseInt-下的数值转换"><a href="#parseInt-下的数值转换" class="headerlink" title="parseInt()下的数值转换"></a><code>parseInt()</code>下的数值转换</h3><p><code>parseInt(param,10)</code>指定参数避免进制转换错误的出现</p>
<p>更好的,请使用<code>Numnber()</code>对字符串进行转换</p>
<p>区别:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">parseInt</span>(<span class="string">'20px'</span>);<span class="comment">//20</span></div><div class="line"><span class="built_in">Number</span>(<span class="string">'20px'</span>);<span class="comment">//NaN</span></div></pre></td></tr></table></figure>
<h3 id="编码规范"><a href="#编码规范" class="headerlink" title="编码规范"></a>编码规范</h3><p><a href="http://cherryblog.site/developing-guideline.html#more" target="_blank" rel="external">这里有一份总结,比较全面</a></p>

                <hr>
                <ul class="pager">
                    
                    <li class="previous">
                        <a href="/2017/06/13/2017-note/12.node笔录/" data-toggle="tooltip" data-placement="top"
                           title="node笔录">&larr; 上一页</a>
                    </li>
                    
                    
                    <li class="next">
                        <a href="/2017/06/11/2017-note/10.闭包测试/" data-toggle="tooltip" data-placement="top"
                           title="闭包测试">下一页 &rarr;</a>
                    </li>
                    
                </ul>
                <div class="comment">
                    <div id="cloud-tie-wrapper" class="cloud-tie-wrapper"></div>
                </div>
            </div>
            <div class="hidden-xs col-sm-3 toc-col">
                <div class="toc-wrap">
                    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#说明"><span class="toc-text">说明</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#最小全局变量Minimizing-Global"><span class="toc-text">最小全局变量Minimizing Global</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#几种不经意之间声明的全局变量示例-反例"><span class="toc-text">几种不经意之间声明的全局变量示例(反例)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#隐式全局变量与明确定义的全局变量的差别"><span class="toc-text">隐式全局变量与明确定义的全局变量的差别</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#全局对象的访问"><span class="toc-text">全局对象的访问</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#声明变量的小技巧-单var形式"><span class="toc-text">声明变量的小技巧:单var形式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#预解析-var的散布问题-Hosting-A-Problem-with-Scattered-vars"><span class="toc-text">预解析:var的散布问题(Hosting:A Problem with Scattered vars)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#for循环的优化-For-Loops"><span class="toc-text">for循环的优化(For Loops)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#for-in循环-for-in-loops"><span class="toc-text">for-in循环(for in loops)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#内置原型的扩展"><span class="toc-text">内置原型的扩展</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#switch模式"><span class="toc-text">switch模式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#避免隐式类型判断"><span class="toc-text">避免隐式类型判断</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#避免eval"><span class="toc-text">避免eval()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#parseInt-下的数值转换"><span class="toc-text">parseInt()下的数值转换</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#编码规范"><span class="toc-text">编码规范</span></a></li></ol>
                </div>
            </div>
        </div>

        <div class="row">
            <!-- Sidebar Container -->

            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                sidebar-container">

                <!-- Featured Tags -->
                

                <!-- Friends Blog -->
                
            </div>
        </div>

    </div>
</article>
<!-- 网易云音乐-->

<iframe class="cloud-music" frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 
    src="//music.163.com/outchain/player?type=2&amp;id=18803411&amp;auto=0&amp;height=66">
</iframe>
<!-- 网易云跟帖-->
<script src="https://img1.cache.netease.com/f2e/tie/yun/sdk/loader.js"></script>
<script>
var cloudTieConfig = {
  url: document.location.href, 
  sourceId: "",
  productKey: "40a8ee22b6084862a8907e6902e525fa",
  target: "cloud-tie-wrapper"
};
var yunManualLoad = true;
Tie.loader("aHR0cHM6Ly9hcGkuZ2VudGllLjE2My5jb20vcGMvbGl2ZXNjcmlwdC5odG1s", true);
</script>

<!-- Footer -->
<!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1 text-center">
                <p class="copyright text-muted">
                    Copyright &copy; MillZhang 2017
                    <br>
                </p>
            </div>
        </div>
    </div>
</footer>

<!-- jQuery -->
<script src="/js/jquery.min.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="/js/bootstrap.min.js"></script>

<!-- Custom Theme JavaScript -->
<script src="/js/blog.js"></script>

<!-- async load function -->
<script>
    function async(u, c) {
      var d = document, t = 'script',
          o = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      o.src = u;
      if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
      s.parentNode.insertBefore(o, s);
    }
</script>

<!-- jquery.tagcloud.js -->
<script>
    // only load tagcloud.js in tag.html
    if($('#tag_cloud').length !== 0){
        async("http://oritfw5nq.bkt.clouddn.com/jquery.tagcloud.js",function(){
            $.fn.tagcloud.defaults = {
                //size: {start: 1, end: 1, unit: 'em'},
                color: {start: '#bbbbee', end: '#0085a1'},
            };
            $('#tag_cloud a').tagcloud();
        })
    }
</script>

<!--fastClick.js -->
<script>
    async("//cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js", function(){
        var $nav = document.querySelector("nav");
        if($nav) FastClick.attach($nav);
    })
</script>



<!--wechat title img-->
<img class="wechat-title-img" src="http://oritfw5nq.bkt.clouddn.com/avatar.jpg?imageslim">
<a id="backtop" class="border-bottom" href="javascript:;">回顶部</a>
<script type="application/javascript">
var docOuter = document.body.scrollTop ? document.body : document.documentElement;
var backToTop = function(rate) {
  var doc = document.body.scrollTop ? document.body : document.documentElement;
  var scrollTop = doc.scrollTop;
  var top = function() {
    scrollTop = scrollTop + (0 - scrollTop) / (rate || 2);
    if (scrollTop < 1) {
      doc.scrollTop = 0;
      return;
    }
    doc.scrollTop = scrollTop;
    // 动画gogogo!
    requestAnimationFrame(top);
  };
  top();
};
var topBtn = document.getElementById('backtop'),
    docTop = docOuter.scrollTop;
if(docTop>=300){
  topBtn.style.opacity = 1;
}

topBtn.onclick = function() {
  backToTop(12)
}
window.onscroll = function(e) {
  var doc = document.body.scrollTop ? document.body : document.documentElement;
  var scrollTop = doc.scrollTop;
  if (scrollTop >= 300) {
    topBtn.style.opacity = 1;
  } else {
    topBtn.style.opacity = 0;
  }
}
</script>
</body>

</html>
